package com.hspedu.newData.dmsxl.动态规划.基础;

import org.junit.Test;

/**
 * @Author: yh
 * @description: 
 * @CreateTime: 2025-05-04 
 * @Version: 1.0
 */

public class LC152乘积最大子数组 {


    // todo
    // 和53很相似，但是当超出范围时需要直接赋值，那个操作是为了什么还没搞明白
    public int maxProduct(int[] nums) {

        int MAX = nums[0];
        int len = nums.length;
        long[] dpMax = new long[len];
        long[] dpMin = new long[len];

        for (int i = 0; i < len; i++) {
            dpMax[i] = nums[i];
            dpMin[i] = nums[i];
        }

        for (int i = 1; i < len; i++) {
            dpMax[i] = Math.max(dpMax[i-1]*nums[i],Math.max(dpMin[i-1]*nums[i],nums[i]));
            dpMin[i] = Math.min(dpMin[i-1]*nums[i],Math.min(dpMax[i-1]*nums[i],nums[i]));
//            if (dpMin[i] < (-1)<<31){
//                dpMin[i] = nums[i];
//            }
            MAX = Math.max(MAX,(int)dpMax[i]);
        }
        return MAX;
    }

    @Test
    public void test() {
        // 测试代码


        maxProduct(new int[]{2,-5,-2,-4,3});

    }


}
